<?php
namespace app\store\model;
use app\common\model\Setting as SettingModel;
use think\Image;
use think\Cache;

/**
 * 系统设置模型
 */
class Setting extends SettingModel
{
    /**
     * 设置项描述
     */
    private $describe = [
		'mode' => '功能设置',
        'sms' => '短信通知',
		'tpl' => '订阅消息',
        'storage' => '上传设置',
        'store' => '站点设置',
        'trade' => '交易设置',
		'printer' => '打印设置',
		'recharge' => '充值设置',
		'delivery' => '配送设置',
		'dealer' => '分销设置',
		'poster' => '海报设置',
    ];

    /**
     * 更新系统设置
     */
    public function edit($key, $values)
    {
		//如果是设置订阅消息
		if($key == 'tpl'){
			//验证是否添加餐饮服务类目
			if($msg = is_food_category()){
				$this->error = $msg;
				return false;	
			}
			$values = $this->tpl($values);
		}
		//如果是设置海报
		if($key == 'poster'){
			if(empty($values['shop']['logo'])){
				$this->error = '门店图标不存在，请补充门店信息';
				return false;
			}
			$this->poster($values);
		}
        $model = self::detail($key) ?: $this;
        // 删除系统设置缓存
        Cache::rm('setting_' . self::$wxapp_id);
        return $model->save([
            'key' => $key,
            'describe' => $this->describe[$key],
            'values' => $values,
            'wxapp_id' => self::$wxapp_id,
        ]) !== false;
    }
	
	/**
     * 服务类目设置
     */
	private function tpl($values)
	{
		$access_token = getAccessToken();
		//获取帐号下的模板列表
		$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token='.$access_token;
		$res = json_decode(curl($url),true);
		if($res['errcode']==0){
			//循环删除订阅消息模板
			for($n=0;$n<sizeof($res['data']);$n++){
				$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token='.$access_token;
				$data = '{"priTmplId":"'.$res['data'][$n]['priTmplId'].'"}';
				$result = json_decode(http_request($url,$data),true);
			}
		}
		//添加订单完成通知
		$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token;
		$data = '{"tid":"677","kidList":[13,2,7,11,12],"sceneDesc":"订单完成通知"}';
		$result = json_decode(http_request($url,$data),true);
		if($result['errcode']==0){
			$values['finish'] = $result['priTmplId'];
		}
		//添加取餐提醒
		$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token;
		$data = '{"tid":"250","kidList":[12,2,4,13,16],"sceneDesc":"取餐提醒"}';
		$result = json_decode(http_request($url,$data),true);
		if($result['errcode']==0){
			$values['take'] = $result['priTmplId'];
		}
		//添加送餐进度提醒
		$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token;
		$data = '{"tid":"7511","kidList":[1,3,4,6,5],"sceneDesc":"送餐进度提醒"}';
		$result = json_decode(http_request($url,$data),true);
		if($result['errcode']==0){
			$values['remind'] = $result['priTmplId'];
		}
		//添加订单配送通知
		$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token;
		$data = '{"tid":"584","kidList":[1,2,7,12,13],"sceneDesc":"订单配送通知"}';
		$result = json_decode(http_request($url,$data),true);
		if($result['errcode']==0){
			$values['delivery'] = $result['priTmplId'];
		}
		return $values;
	}
	
	/**
     * 合成商家海报模板
     */
	private function poster($values)
	{
		
		header('Content-Type: image/png');//输出协议头
		
		//$dst_path = '4.png';//背景图
		$bg_path = 'https://food.cityphp.cn/assets/store/img/dealer/poster.png';//$values['backdrop']['src'];//背景图
		//生成临时缩略图
		$image = Image::open('./uploads/'.$values['shop']['file_name']);//读取原图
		$url = './assets/poster/temp/'.'shop_logo'.$values['shop']['shop_id'].'.png';
		$image->thumb(60,60)->save($url);
		yuan_img($url);
		
	
		//创建图片的实例
		$bg = imagecreatefromstring(file_get_contents($bg_path));//读取背景图片数据流
		$logo = imagecreatefromstring(file_get_contents($url));//读取二维码数据流
		//获取水印图片的宽高
		list($src_w, $src_h) = getimagesize($url);
		
		//将水印图片复制到目标图片上，最后个参数100是设置透明度，这里实现不透明效果
		//imagecopymerge($bg, $logo, 1360, 833, 0, 0, $src_w, $src_h, 100);
		//如果水印图片本身带透明色，则使用imagecopy方法
		imagecopy($bg, $logo, 40, 976, 0, 0, $src_w, $src_h);
	
		//设置水印文字颜色
		//SIMYOU.TTF 是幼圆字体
		$col = imagecolorallocatealpha($bg,255,255,255,0);
		//添加水印文字
		//30 是字体大小
		//215横坐标
		//875 980 是纵坐标
		$b = strlen($values['shop']['shop_name']);//计算有几个汉子
		$x = (750-$b/3*55)/2;
		imagettftext($bg,40,0,$x,325,$col,"assets/plugins/fonts/zhongjianhei.ttf",$values['shop']['shop_name']);
		$col = imagecolorallocatealpha($bg,0,0,0,0);
		imagettftext($bg,15,0,110,1020,$col,"assets/plugins/fonts/msyh.ttf",$values['shop']['shop_name']);
		$address = '地址：'.$values['shop']['address'];
		$x = 110+$b/3*23+20;
		imagettftext($bg,12,0,$x,1005,$col,"assets/plugins/fonts/msyh.ttf",$address);
		$phone = '电话：'.$values['shop']['phone'];
		imagettftext($bg,12,0,$x,1035,$col,"assets/plugins/fonts/msyh.ttf",$phone);
		
		//输出图片
		//list($dst_w, $dst_h, $dst_type) = getimagesize($bg);
		imagepng($bg,'assets/poster/'.self::$wxapp_id.'.png');
		//将数据进行销毁
		imagedestroy($bg);
		imagedestroy($logo);
	}


}
